package com.example.campusrear.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.campusrear.Service.ActivityRecordService;
import com.example.campusrear.Service.ActivityService;
import com.example.campusrear.Utils.R;
import com.example.campusrear.entity.Activity;
import com.example.campusrear.entity.ActivityRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class activityController {
    @Resource
    private ActivityService activityService;

    @Resource
    private ActivityRecordService activityRecordService;

    @GetMapping("/page")
    public R<Page<Activity>> page(int page,int pageSize,String name){
        Page<Activity> activityPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Activity::getActivityName,name);
        activityService.page(activityPage,wrapper);
        return R.success(activityPage);
    }
    @PostMapping("/application")
    public R<String> application(@RequestBody ActivityRecord activityRecord){
        Long activityId = activityRecord.getActivityId();
        Long studentId = activityRecord.getStudentId();
        LambdaQueryWrapper<ActivityRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityRecord::getActivityId,activityId);
        wrapper.eq(ActivityRecord::getStudentId,studentId);
        ActivityRecord one = activityRecordService.getOne(wrapper);
        if (one!=null){
            return R.error("已报名，不能再次报名");
        }
        activityRecordService.save(activityRecord);
        Activity activity = activityService.getById(activityId);
        Integer activityNum = activity.getActivityNum();
        Integer regNum = activity.getRegNum();
        Integer currentNum = activity.getCurrentNum();
        regNum = ++regNum;
        currentNum = activityNum-regNum;
        activity.setRegNum(regNum);
        activity.setCurrentNum(currentNum);
        activityService.updateById(activity);
        return R.success("报名成功");
    }

    @PostMapping("/save")
    public R<String> save(@RequestBody Activity activity){
        activity.setRegNum(activity.getActivityNum());
        activity.setCurrentNum(0);
        activityService.save(activity);
        return R.success("增加成功");
    }

    @PostMapping("/update")
    public R<String> update(@RequestBody Activity activity){
        activityService.updateById(activity);
        return R.success("修改成功");
    }



    @DeleteMapping("/del/{aId}")
    public R<String> delete(@PathVariable Long aId){
        LambdaQueryWrapper<ActivityRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityRecord::getActivityId,aId);
        activityRecordService.remove(wrapper);
        activityService.removeById(aId);
        return R.success("删除成功");
    }

}
